package com.yyfax.pay.quartz.fixed;

import org.quartz.Job;
import org.quartz.SchedulerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.yyfax.pay.quartz.dynamic.DynamicSchedulerFactory;
import com.yyfax.pay.quartz.model.vo.DynamicJobVO;

public class QuartzTaskUtils {

	private static Logger logger = LoggerFactory.getLogger(QuartzTaskUtils.class);

	public static DynamicJobVO createJob(String cornEx, String jobName, Class<Job> job, boolean isDuplicate) {

		DynamicJobVO dynamicJob = new DynamicJobVO();
		dynamicJob.cronExpression(cornEx);
		dynamicJob.target(job);
		dynamicJob.jobName(jobName);
		try {
			if (isDuplicate) {
				// 如果重复下单，则更新调度机制
				DynamicSchedulerFactory.updateJob(dynamicJob);
			} else {
				DynamicSchedulerFactory.registerJob(dynamicJob);
			}
		} catch (SchedulerException e) {
			throw new IllegalStateException(e);
		} finally {
			logger.info("【添加异步通知任务】{}", job.getName());
			logger.info("【添加异步通知任务Quartz】{}", dynamicJob);
		}
		return dynamicJob;
	}

}
